package com.bw.a2209clubshop.ui.login

import android.view.MotionEvent
import androidx.activity.viewModels
import androidx.core.view.get
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter

import com.bw.a2209clubshop.R
import com.bw.a2209clubshop.base.BaseToolbarActivity
import com.bw.a2209clubshop.databinding.ActivityLoginBinding
import com.bw.a2209clubshop.ui.login.viewModel.LoginViewModel
import com.bw.a2209clubshop.utils.Router
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

@Route(path = Router.Ui.LoginActivity)
@AndroidEntryPoint
class LoginActivity : BaseToolbarActivity<LoginViewModel,ActivityLoginBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_login
    private val mViewModel: LoginViewModel by viewModels()
    private lateinit var mNavController: NavController //导航控制器

    companion object {
        fun jump() {
            ARouter.getInstance().build(Router.Ui.LoginActivity).withTransition(R.anim.bottom_in, R.anim.fab_out).navigation()
        }
    }
    override fun initView() {
        super.initView()
        mNavController = (supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment).navController

    }

    override fun initListener() {
        mToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.action) {
                mNavController.navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
            }
            true
        }
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                mViewModel.currentFragment.collect {
                    manageFragment(it)
                }
            }
        }

//        observeEvent<WxAttach2PhoneEvent>(isSticky = true) {
//            mViewModel.wxOpenId = it.wxOpenId
//            mViewModel.gender = it.gender
//            mViewModel.nickName = it.nickName
//            mViewModel.headImg = it.headImg
//            //跳转绑定手机号
//            mNavController.navigate(R.id.action_phoneLoginFragment_to_bindPhoneFragment)
//        }
    }

//    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
//        if (ev!!.action == MotionEvent.ACTION_DOWN) {
//
//            postEvent(this, ClickAreaEvent(ev))
//            return super.dispatchTouchEvent(ev)
//        }
//
//        return if (window.superDispatchTouchEvent(ev)) {
//            true
//        } else super.dispatchTouchEvent(ev)
//    }


    private fun manageFragment(it: String) {
        title = ""
        mToolbar.menu.clear()
        when (it) {
            PhoneLoginFragment::class.simpleName -> {
                mToolbar.setNavigationIcon(R.mipmap.icon_exit)
                mToolbar.inflateMenu(R.menu.single_menu)
                mToolbar.menu[0].title = "密码登录"
                mToolbar.menu[0].icon = null
            }
            else -> {
                mToolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
            }

        }
    }

    override fun initData() {

    }


//    fun handleLoginSuccess() {
//        //发送登录成功消息
//        postEvent(LoginEvent())
//        finish()
//    }

//    override fun onDestroy() {
//        super.onDestroy()
//        clearStickyEvent(WxAttach2PhoneEvent::class.java)
//    }

    class ClickAreaEvent(val ev: MotionEvent)



}